﻿using DBMode;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.ComponentModel.Design.Serialization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CMS.ViewModels
{
    public class MainViewModel:BindableBase
    {
        private readonly IRegionManager regionManager;
        private readonly IDialogService dialogService;

        public MainViewModel(IRegionManager regionManager,IDialogService dialogService)
        {
            this.regionManager = regionManager;
            this.dialogService = dialogService;
            this.Member = AppInstance.GetInstance.Member;
            Member.Name = "root";
            Member.Password = "admin";
        }
        private Member member;

        public Member Member
        {
            get { return member; }
            set { member = value; RaisePropertyChanged(); }
        }
        /// <summary>
        /// 初始时刻显示IndexView
        /// </summary>
        public void Nav() {
            regionManager.Regions["MainViewRegion"].RequestNavigate("IndexView");
        }

        public DelegateCommand<object> OpenCommand { get { return new DelegateCommand<object>(Open); } }

        public DelegateCommand LogoutCommand { get { return new DelegateCommand(Logout); } }
        /// <summary>
        /// 点击头像退出到登录界面
        /// </summary>
        /// <exception cref="NotImplementedException"></exception>
        private void Logout()
        {
            App.Current.MainWindow.Hide();
            dialogService.ShowDialog("LoginView");
            App.Current.MainWindow.Show();
        }

        private void Open(object obj)
        {
            if (obj == null) return;
            if (obj.ToString().Equals("RecordView") && (AppInstance.GetInstance.Member.Role == 1)) {
                MessageBox.Show("请登录管理员账号");
            }
            regionManager.Regions["MainViewRegion"].RequestNavigate(obj.ToString());
        }
    }
}
